﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using SystemModels;
using SystemBLL;

namespace SystemUI
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 用户对象
        /// </summary>
        public static Users _user;
        /// <summary>
        /// 登录事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            //数据校验
            if (Check())
            {
                try
                {
                    _user = new UserBLL().CheckUser(int.Parse(txtName.Text.Trim()), txtPwd.Text.Trim());
                    if (_user == null) return;
                    if (_user.UserStatus == 1)
                    {
                        MessageBox.Show("该用户已经登录，请确认后重新登录！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtName.SelectAll();
                        this.txtName.Focus();
                        return;
                    }
                    else
                    {
                        _user.UserStatus = 1;
                        new UserBLL().ChangeUser(_user);
                    }
                }
                catch (Exception exception)
                {

                    MessageBox.Show(exception.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                finally
                {
                    //判断用户是否为空
                    if (_user != null)
                    {
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("该用户不存在！！");
                        this.DialogResult = System.Windows.Forms.DialogResult.None;
                    }
                }
            }
        }

        #region 界面用户校验
        /// <summary>
        /// 界面用户校验
        /// </summary>
        /// <returns>bool</returns>
        private bool Check()
        {
            if (string.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                MessageBox.Show("用户编号不能为空！");
                this.txtName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(this.txtPwd.Text.Trim()))
            {
                MessageBox.Show("用户密码不能为空！");
                this.txtPwd.Focus();
                return false;
            }
            int id = 0;
            if (!int.TryParse(this.txtName.Text.Trim(), out id))
            {
                MessageBox.Show("用户编号必须是数字！！");
                this.txtName.Focus();
                return false;
            }
            else
            {
                return true;
            }
        }
        #endregion

        #region 取消登录
        /// <summary>
        /// 取消登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        #endregion

        #region 窗口可拖动
        Point mouseOff;//鼠标移动位置变量
        bool leftFlag;//标签是否为左键

        /// <summary>
        /// 鼠标左键按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Login_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                leftFlag = true;                  //点击左键按下时标注为true;
            }
        }

        /// <summary>
        /// 鼠标左键放开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Login_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后标注为false;
            }
        }

        /// <summary>
        /// 鼠标左键按住移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Login_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                Location = mouseSet;
            }
        }
        #endregion
    }
}
